package com.ibl.rover.lite.client.Sync.Parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.ibl.rover.lite.client.Logger;
import com.ibl.rover.lite.client.Application.ApplicationContext;
import com.ibl.rover.lite.client.Application.Properties.ApplicationProperties;
import com.ibl.rover.lite.client.Sync.Parser.SyncEntity.OperationType;
import com.ibl.rover.lite.client.Sync.Parser.SyncXMLParser.UpdateObserver;
import com.ibl.rover.lite.client.Sync.Process.SyncronizationProcess;
import com.ibl.rover.lite.client.Utility.Utility;
import com.ibl.rover.lite.client.constants.RoverConstants;
import com.ibl.rover.lite.client.constants.RoverConstants.APPSETUP;
import com.ibl.rover.lite.client.constants.RoverConstants.CUSTOMER;
import com.ibl.rover.lite.client.constants.RoverConstants.PRODUCT;
import com.ibl.rover.lite.client.constants.RoverConstants.PRODUCTCATEGORY;
import com.ibl.rover.lite.client.constants.RoverConstants.ROUTE;
import com.ibl.rover.lite.client.constants.RoverConstants.SyncProgress;
import com.ibl.rover.lite.common.bean.AppSetupBean;
import com.ibl.rover.lite.common.bean.CustomerBean;
import com.ibl.rover.lite.common.bean.ProductBean;
import com.ibl.rover.lite.common.bean.ProductCategroy;
import com.ibl.rover.lite.common.bean.RouteBean;
import com.ibl.rover.lite.common.db.BaseException;
import com.ibl.rover.lite.common.db.DatabaseManager;

public class SyncDataUpdateObserver implements UpdateObserver {

	private ApplicationContext roverApp;
	private DatabaseManager manager;
	private ApplicationProperties appParams;
	private SyncronizationProcess syncronizationProcess;
	private Logger logger = Logger.getLogger();

	public SyncDataUpdateObserver(ApplicationContext roverApp,
			SyncronizationProcess syncronizationProcess) {

		this.roverApp = roverApp;
		this.manager = roverApp.getDatabaseManager();
		this.appParams = roverApp.getAppParams();
		this.syncronizationProcess = syncronizationProcess;

	}

	@Override
	public void onStartDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpdate(String entityName, OperationType opType,
			List<Map<String, String>> attributeSet) {
		try {
			if (entityName.equalsIgnoreCase(RoverConstants.APPSETUP)) {
				doHandleAppSetup(opType, attributeSet);
			} else if (entityName.equalsIgnoreCase(RoverConstants.CUSTOMERS)) {
				doHandleCustomer(opType, attributeSet);
			} else if (entityName.equalsIgnoreCase(RoverConstants.PRODUCT)) {
				doHandleProduct(opType, attributeSet);
			} else if (entityName
					.equalsIgnoreCase(RoverConstants.PRODUCTS_CATEGORY)) {
				doHandleProductCategory(opType, attributeSet);
			} else if (entityName.equalsIgnoreCase(RoverConstants.ROUTE)) {
				doHandleRoute(opType, attributeSet);
			}
		} catch (Exception ex) {
			logger.logException("[Exception occured while Parsing Xml", ex);
			ex.printStackTrace();
		}

	}

	private void doHandleRoute(OperationType operationType,
			List<Map<String, String>> attributeList) {
		switch (operationType) {
		case EAdd: {
			if (0 < attributeList.size()) {
				try {
					if (null != syncronizationProcess) {
						syncronizationProcess
								.doProgress(SyncProgress.SYNC_ROUTES);
					}
					List<RouteBean> routeBeanList = getRouteBeanList(attributeList);

					manager.saveBean(routeBeanList);
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			break;

		default:
			break;
		}

	}

	private List<RouteBean> getRouteBeanList(
			List<Map<String, String>> attributeList) {
		try {
			List<RouteBean> routeBeanList = new ArrayList<RouteBean>(
					attributeList.size());
			for (Map<String, String> attributeMap : attributeList) {
				RouteBean routeBean = new RouteBean();

				String xDay = attributeMap.get(ROUTE.xDay);
				String xRoute = attributeMap.get(ROUTE.xRoute);
				if (!Utility.isEmpty(xRoute))
					routeBean.setCode(xRoute);
				if (!Utility.isEmpty(xDay))
					routeBean.setDay(xDay);

				routeBeanList.add(routeBean);
			}
			return routeBeanList;
		} catch (Exception e) {
			logger.logException("getCustomer Exception", e);
		}
		return null;
	}

	private void doHandleProductCategory(OperationType operationType,
			List<Map<String, String>> attributeList) {
		switch (operationType) {
		case EAdd: {
			if (0 < attributeList.size()) {
				try {
					if (null != syncronizationProcess) {
						syncronizationProcess
								.doProgress(SyncProgress.SYNC_PRODUCT_CATEGORY);
					}
					List<ProductCategroy> productCategroyList = getProductCategroyList(attributeList);

					manager.saveBean(productCategroyList);
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			break;

		default:
			break;
		}

	}

	private List<ProductCategroy> getProductCategroyList(
			List<Map<String, String>> attributeList) {
		try {
			List<ProductCategroy> productCategroyList = new ArrayList<ProductCategroy>(
					attributeList.size());
			for (Map<String, String> attributeMap : attributeList) {
				ProductCategroy productCategroy = new ProductCategroy();

				String CATEGCODE = attributeMap.get(PRODUCTCATEGORY.CATEGCODE);
				if (!Utility.isEmpty(CATEGCODE))
					productCategroy.setCategoryCode(CATEGCODE);

				productCategroyList.add(productCategroy);
			}
			return productCategroyList;
		} catch (Exception e) {
			logger.logException("getCustomer Exception", e);
		}
		return null;
	}

	private void doHandleCustomer(OperationType operationType,
			List<Map<String, String>> attributeList) {
		switch (operationType) {
		case EAdd: {
			if (0 < attributeList.size()) {
				try {
					if (null != syncronizationProcess) {
						syncronizationProcess
								.doProgress(SyncProgress.SYNC_CUSTOMER);
					}
					List<CustomerBean> customerList = getCustomerList(attributeList);

					manager.saveBean(customerList);
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			break;

		default:
			break;
		}

	}

	private void doHandleProduct(OperationType operationType,
			List<Map<String, String>> attributeList) {
		switch (operationType) {
		case EAdd: {
			if (0 < attributeList.size()) {
				try {
					if (null != syncronizationProcess) {
						syncronizationProcess
								.doProgress(SyncProgress.SYNC_PRODUCTS);
					}
					List<ProductBean> productList = getProductList(attributeList);

					manager.saveBean(productList);
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			break;

		default:
			break;
		}

	}

	private List<CustomerBean> getCustomerList(
			List<Map<String, String>> attributeList) {
		try {
			List<CustomerBean> customerBeanList = new ArrayList<CustomerBean>(
					attributeList.size());
			for (Map<String, String> attributeMap : attributeList) {
				CustomerBean customerBean = new CustomerBean();

				// String code = attributeMap.get(APPSETUP.TSO_NAME);
				// String lastOrder = attributeMap.get(APPSETUP.LAST_ORDER_NO);

				String ADDRESS = attributeMap.get(CUSTOMER.ADDRESS);
				String CLASS_CODE = attributeMap.get(CUSTOMER.CLASS_CODE);
				String CODE = attributeMap.get(CUSTOMER.CODE);
				String CREDIT_LIMIT = attributeMap.get(CUSTOMER.CREDIT_LIMIT);
				String NAME = attributeMap.get(CUSTOMER.NAME);
				String ROU_NUMBER = attributeMap.get(CUSTOMER.ROU_NUMBER);
				String position= attributeMap.get(CUSTOMER.POSITION);

				if (!Utility.isEmpty(ADDRESS))
					customerBean.setAddress(ADDRESS);
				if (!Utility.isEmpty(CODE))
					customerBean.setCode(CODE);
				if (!Utility.isEmpty(CREDIT_LIMIT))
					customerBean.setCreditLimit(Double
							.parseDouble(CREDIT_LIMIT));
				if (!Utility.isEmpty(NAME))
					customerBean.setName(NAME);
				if (!Utility.isEmpty(ROU_NUMBER))
					customerBean.setRouteNumber(Integer.parseInt(ROU_NUMBER));
				if (!Utility.isEmpty(position))
					customerBean.setPosition(Integer.parseInt(position));
				
				customerBeanList.add(customerBean);
			}
			return customerBeanList;
		} catch (Exception e) {
			logger.logException("getCustomer Exception", e);
		}
		return null;
	}

	private void doHandleAppSetup(OperationType operationType,
			List<Map<String, String>> attributeList) {
		switch (operationType) {
		case EAdd: {
			if (0 < attributeList.size()) {
				try {
					if (null != syncronizationProcess) {
						syncronizationProcess
								.doProgress(SyncProgress.SYNC_APPSETUP);
					}
					List<AppSetupBean> apSetupList = getApSetupList(attributeList);

					manager.saveBean(apSetupList);
				} catch (BaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			break;

		default:
			break;
		}

	}

	private List<ProductBean> getProductList(
			List<Map<String, String>> attributeList) {
		try {
			List<ProductBean> productBeanList = new ArrayList<ProductBean>(
					attributeList.size());
			for (Map<String, String> attributeMap : attributeList) {
				ProductBean productBean = new ProductBean();

				String BRANDCODE = attributeMap.get(PRODUCT.BRANDCODE);
				String CATEGCODE = attributeMap.get(PRODUCT.CATEGCODE);
				String FULLDESC = attributeMap.get(PRODUCT.FULLDESC);
				String UNITPRICE = attributeMap.get(PRODUCT.UNITPRICE);
				String CODE = attributeMap.get(PRODUCT.CODE);
				String DISP_SEQ = attributeMap.get(PRODUCT.DISP_SEQ);

				if (!Utility.isEmpty(BRANDCODE)) {
					productBean.setBrandCode(BRANDCODE);
					productBean.setProductCode(CODE);
				}
				if (!Utility.isEmpty(CATEGCODE))
					productBean.setProductCategoryId(CATEGCODE);
				if (!Utility.isEmpty(FULLDESC))
					productBean.setDescription(FULLDESC);
				if (!Utility.isEmpty(UNITPRICE))
					productBean.setUnitPrice(UNITPRICE);
				if (!Utility.isEmpty(DISP_SEQ))
					productBean.setDisplaySequence(Long.parseLong(DISP_SEQ));

				productBeanList.add(productBean);
			}
			return productBeanList;
		} catch (Exception e) {
			logger.logException("getCustomer Exception", e);
		}
		return null;
	}

	private List<AppSetupBean> getApSetupList(
			List<Map<String, String>> attributeList) {
		try {
			List<AppSetupBean> appSetupBeanList = new ArrayList<AppSetupBean>(
					attributeList.size());
			for (Map<String, String> attributeMap : attributeList) {
				AppSetupBean appSetupBean = new AppSetupBean();

				String code = attributeMap.get(APPSETUP.TSO_NAME);
				String lastOrder = attributeMap.get(APPSETUP.LAST_ORDER_NO);

				if (!Utility.isEmpty(code))
					appSetupBean.setCode(code);
				if (!Utility.isEmpty(lastOrder))
					appSetupBean.setLastOrder(lastOrder);
				appSetupBean.setName("");
				appSetupBeanList.add(appSetupBean);
			}
			return appSetupBeanList;
		} catch (Exception e) {
			logger.logException("getApSetupList Exception", e);
		}
		return null;
	}

	@Override
	public void onEndDocument() {
		// TODO Auto-generated method stub

	}

}
